package com.peng.config;

import com.peng.service.AiAssistant;
import com.peng.service.ChatAssistant;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.memory.chat.TokenWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author love_ovo
 * @ClassName ChatLanguageModelConfig.java
 * @createTime 2025年05月13日 17:33:00
 */
@Configuration(proxyBeanMethods = false)
public class ChatLanguageModelConfig {
    @Bean
    public ChatLanguageModel chatLanguageModel() {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("DASH_SCOPE_API_KEY"))
                //普通文本换长模型
                .modelName("qwen-long")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }

    // 这个是普通文本模型
    @Bean
    public ChatAssistant chatAssistant(ChatLanguageModel chatLanguageModel) {
        return AiServices.create(ChatAssistant.class, chatLanguageModel);
    }

    // 这个是带记忆功能的模型
    @Bean
    public AiAssistant aiAssistant(ChatLanguageModel chatLanguageModel) {
        return AiServices.builder(AiAssistant.class)
                .chatLanguageModel(chatLanguageModel)
                //这里就是设置了记忆缓存功能
                //设置了最大个数为100，超过100会删除最早的一条数据
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(100))
                .build();
    }
}